package name.wilbur.course.db.bigone.ui;

import java.util.Iterator;
import java.util.Map;

import name.wilbur.course.db.bigone.control.MainMgr;
import name.wilbur.course.db.bigone.model.Article;
import name.wilbur.course.db.bigone.model.BaseDBModel;
import name.wilbur.course.db.bigone.model.User;
import name.wilbur.course.db.bigone.model.UserProfileView;
import name.wilbur.course.db.bigone.ui.custom.ListContentProvider;
import name.wilbur.course.db.bigone.ui.custom.ListLabelProvider;
import name.wilbur.course.db.bigone.ui.custom.ListModel;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MainWin extends BaseWindow {
	private MainMgr mgr;
//	private Shell sShell = null;  //  @jve:decl-index=0:visual-constraint="11,45"
	protected ListViewer logList, friendList, usersList;  //  @jve:decl-index=0:
	private Group logGroup, friendGroup, configGroup, controlGroup, searchGroup;
	private ListModel<BaseDBModel> logModel, friendModel;  //  @jve:decl-index=0:
	private Combo searchFriendC;
	private User currFriend;
	private UserProfileView currentSearchedFriend;
	protected Article currArticle;
	private MainWin thisWin = this;  //  @jve:decl-index=0:
	
	private Text searchFriendNameT;
	
	public MainWin(Map<String, Object> session) {
		mgr = new MainMgr(session);
	}
	public MainWin(Display disp, Map<String, Object> session) {
		super(disp);
		mgr = new MainMgr(session);
	}
	/**
	 * This method initializes sShell
	 */
	public void createSShell() {
		final GridData hFillGridData = new GridData(GridData.FILL_HORIZONTAL);
		final GridData oneSpanGridData = new GridData(GridData.FILL, GridData.FILL, true, false, 1, 1);
		final GridData twoSpanGridData = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
		final GridData twoSpanGridDataVEnd = new GridData(SWT.FILL, SWT.END, true, false, 2, 1);
		sShell = new Shell();
		sShell.setText("Main Window");
		GridData gridData;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 7;
		gridLayout.makeColumnsEqualWidth = true;
		sShell.setLayout(gridLayout);
		sShell.setSize(new Point(800, 500));
//		sShell.setBackgroundImage(new Image(null, "images/x1.jpg"));
		
		gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		// log manage group
		logGroup = new Group(sShell, SWT.None);
		logGroup.setText("日志管理");
		logGroup.setLayout(gridLayout);
		logGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 3, 8));
		// log list
		logList = new ListViewer(logGroup, SWT.SINGLE);
		logList.setContentProvider(new ListContentProvider<User>(logList));
		logList.setLabelProvider(new ListLabelProvider());
		logList.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 7));
		logList.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				Iterator it = selection.iterator();
				if (it.hasNext())
					currArticle = (Article)it.next();
//				System.out.println(currArticle);
			}
		});
		// add log button
		Button addLogB = new Button(logGroup, SWT.None);
		addLogB.setLayoutData(oneSpanGridData);
		addLogB.setText("写日志");
		// remove log button
		addLogB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				ArticleWindow aw = new ArticleWindow(disp, logModel, mgr.getSession(), true);
				aw.open();
			}
		});
		Button removeLogB = new Button(logGroup, SWT.None);
		removeLogB.setLayoutData(oneSpanGridData);
		removeLogB.setText("删除日志");
		removeLogB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				logList.remove(currArticle);
				mgr.deleteArticle(currArticle);
			}
		});
		// modify log button
		Button modifyLogB = new Button(logGroup, SWT.None);
		modifyLogB.setLayoutData(oneSpanGridData);
		modifyLogB.setText("修改日志");
		modifyLogB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				ArticleWindow aw = new ArticleWindow(disp, currArticle, logModel, mgr.getSession(), true);
				aw.open();
			}
		});
		
		gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		
		// friend manage group
		friendGroup = new Group(sShell, SWT.None);
		friendGroup.setText("好友管理");
		friendGroup.setLayout(gridLayout);
		friendGroup.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 8));
		// friend list
		friendList = new ListViewer(friendGroup, SWT.V_SCROLL);
		friendList.setContentProvider(new ListContentProvider<UserProfileView>(friendList));
		friendList.setLabelProvider(new ListLabelProvider());
		friendList.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 7));
		friendList.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				Iterator it = selection.iterator();
				if (it.hasNext())
					currFriend = (User)it.next();
//				System.out.println(currFriend);
			}
		});
		// remove friend button
		Button removeFriendB = new Button(friendGroup, SWT.None);
		removeFriendB.setLayoutData(oneSpanGridData);
		removeFriendB.setText("删除好友");
		removeFriendB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				MessageBox warnBox = new MessageBox(sShell, SWT.YES|SWT.NO);
				warnBox.setText("确认删除操作");
				warnBox.setMessage("确认要删除好友" + currFriend.getName() + "吗?");
				int result = warnBox.open();
				if (result == SWT.YES) {
					friendList.remove(currFriend);
					mgr.deleteFriend(mgr.getMe().getPk(), currFriend.getPk());
				}
			}
		});
		
		// check friend button
		Button checkFriendB = new Button(friendGroup, SWT.None);
		checkFriendB.setLayoutData(oneSpanGridData);
		checkFriendB.setText("查看好友");
		checkFriendB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				UserInfoWin w = new UserInfoWin(disp, currFriend, mgr.getSession());
				w.open();
			}
		});
		
		// search friend group
		searchGroup = new Group(sShell, SWT.BORDER);
		searchGroup.setText("查找好友");
		searchGroup.setLayout(gridLayout);
		searchGroup.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 2, 5));
		// search name label
		Label searchFriendNameL = new Label(searchGroup, SWT.None);
		searchFriendNameL.setLayoutData(oneSpanGridData);
		searchFriendNameL.setText("姓名");
		// search name text
		searchFriendNameT = new Text(searchGroup, SWT.BORDER);
		searchFriendNameT.setLayoutData(hFillGridData);
		// search city label
		Label searchFriendCityL = new Label(searchGroup, SWT.None);
		searchFriendCityL.setLayoutData(oneSpanGridData);
		searchFriendCityL.setText("所在城市");
		// search city combo
		searchFriendC = new Combo(searchGroup, SWT.BORDER);
		searchFriendC.setLayoutData(oneSpanGridData);
		// search button
		Button searchFriendB = new Button(searchGroup, SWT.None);
		searchFriendB.setLayoutData(oneSpanGridData);
		searchFriendB.setText("查找");
		searchFriendB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				usersList.setInput(mgr.searchFriend(searchFriendNameT.getText(), searchFriendC.getText()));
			}
		});
		// add friend button
		Button addFriendB = new Button(searchGroup, SWT.None);
		addFriendB.setLayoutData(oneSpanGridData);
		addFriendB.setText("添加好友");
		addFriendB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				boolean ok = mgr.addFriend(currentSearchedFriend);
				MessageBox warnBox = new MessageBox(sShell, SWT.OK);
				warnBox.setText("添加好友结果");
				if (!ok) {
					warnBox.setMessage("添加好友失败");
					warnBox.open();
				} else {
					warnBox.setMessage("成功添加好友" + currentSearchedFriend.getName());
					warnBox.open();
					friendList.add(currentSearchedFriend.toUser());
				}
			}
		});
		// friend name and city ID
		final String friendName = searchFriendNameT.getText();
		final String cityName = searchFriendC.getText();
		searchFriendB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				mgr.searchFriend(friendName, cityName);
			}
		});
		// users list
		usersList = new ListViewer(searchGroup, SWT.V_SCROLL);
		usersList.setContentProvider(new ListContentProvider<User>(usersList));
		usersList.setLabelProvider(new ListLabelProvider());
		usersList.getList().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 6));
		usersList.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) event.getSelection();
				Iterator it = selection.iterator();
				if (it.hasNext())
					currentSearchedFriend = (UserProfileView)it.next();
//				System.out.println(currentSearchedFriend);
			}
		});
		
		// config buttons group
		configGroup = new Group(sShell, SWT.BORDER);
		configGroup.setText("设置");
		configGroup.setLayout(gridLayout);
		configGroup.setLayoutData(new GridData(GridData.FILL, GridData.END, true, false, 2, 1));
		// modify user info button
		Button modifyUserInfoB = new Button(configGroup, SWT.None);
		modifyUserInfoB.setLayoutData(twoSpanGridDataVEnd);
		modifyUserInfoB.setText("修改个人信息");
		modifyUserInfoB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				PersonalInfoWin piw = new PersonalInfoWin(disp, mgr.getMe());
				piw.open();
			}
		});
		// control buttons group
		controlGroup = new Group(sShell, SWT.BORDER);
		controlGroup.setText("控制");
		controlGroup.setLayout(gridLayout);
		controlGroup.setLayoutData(new GridData(GridData.FILL, GridData.END, true, false, 2, 1));
		// exit button
		Button exitB = new Button(controlGroup, SWT.None);
		exitB.setLayoutData(twoSpanGridDataVEnd);
		exitB.setText("退出");
		exitB.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				close();
			}
		});
	}
	
	public void initWindowData() {
		searchFriendC.setItems(mgr.getAllCitys());
		logModel = new ListModel<BaseDBModel>(mgr.getArticles());
		logList.setInput(logModel);
		
		friendModel = new ListModel<BaseDBModel>(mgr.findAllFriends());
		friendList.setInput(friendModel);
	}
}
